package com.msf.swingdemo.container;

import com.formdev.flatlaf.FlatDarkLaf;
import com.msf.swingdemo.utils.IconUtil;

import javax.swing.*;
import java.awt.*;

public class SplashScreen extends JWindow {

    private int duration;

    public SplashScreen(int d) {
        try {
            UIManager.setLookAndFeel(new FlatDarkLaf());

            duration = d;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void showSplash() {
        JPanel content = (JPanel)getContentPane();
        content.setBackground(Color.white);

        // Set the window's bounds, centering the window.
        int width = 450;
        int height = 115;
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (screen.width - width) / 2;
        int y = (screen.height - height) / 2;
        setBounds(x, y, width, height);

        // Build the splash screen.
        JLabel label = new JLabel(IconUtil.Oreilly_Image);
        JLabel copyrt = new JLabel("Copyright 2002, O'Reilly & Associates", JLabel.CENTER);
        copyrt.setFont(new Font("Sans-Serif", Font.BOLD, 12));
        content.add(label, BorderLayout.CENTER);
        content.add(copyrt, BorderLayout.SOUTH);
        Color oraRed = new Color(156, 20, 20, 255);
        content.setBorder(BorderFactory.createLineBorder(oraRed, 10));

        setVisible(true);

        // Wait a little while, maybe while loading resources.
        try {
            Thread.sleep(duration);
        } catch (Exception ex) {}

        setVisible(false);
    }

    void showSplashAndExit() {
        showSplash();
        System.exit(0);
    }

    public static void main(String[] args) {
        SplashScreen splashScreen = new SplashScreen(5000);
        splashScreen.showSplashAndExit();
    }
}
